
using System;
using System.Globalization;
using UnityEngine;

namespace QQSDK
{

    public enum PurchaseState
    {
        CheckOK = 1,//验证成功，有订单号
        Error = -1, //验证失败，网络请求失败
        CheckPayError = -2, //数据正确，但订单服务端验证失败
        CheckError = -3, //请求验证接口成功，但验证结果未知
    }


    //统一返回接口
    public class PurchaseCallBackData
    {
        public PurchaseState state; //业务使用的时候，根据状态返回不同的提示,
        public string platform = "";
        public string orderId = "";
        public string product_id = "";
       

        //以下参数未必返回，请谨慎使用
        public string purchaseTime = "";
       
        public bool isSandbox = false; //是否是沙盒 ios 可以， Android 不生效
        public bool isTrialPeriod = false; // 是否续订 ios 可以， Android 不生效
    }

    public class GooglePlayPurchaseCallBack
    {
        public string platform = "";
        public string product_id = "";
        public string state = "";
        public string message = "";

        internal PurchaseCallBackData toPurchaseCallBack()
        {
            PurchaseCallBackData pcbd = new PurchaseCallBackData();
            pcbd.platform = platform;
            pcbd.product_id = product_id;
            if (message.Contains("code"))
            {
                GooglePlayCheckInfo googlePlayCheckInfo = JsonMapper.ToObject<GooglePlayCheckInfo>(message);
                if (googlePlayCheckInfo.records.purchaseState == "0")
                {
                    pcbd.state = PurchaseState.CheckOK;
                    pcbd.orderId = googlePlayCheckInfo.records.orderId;

                    pcbd.purchaseTime = googlePlayCheckInfo.records.purchaseTime;
                  
                  
                }
                else
                {
                    pcbd.state = PurchaseState.CheckPayError;//http ok and checkpay error
                }
            }
            else
            {
                if (state == "1")
                {
                    pcbd.state = PurchaseState.CheckError;//http ok and checkcode error
                }
                else
                {
                    pcbd.state = PurchaseState.Error;//http error 404?
                }
            }
            return pcbd;
        }
    }

    //{"code":6000,"msg":"处理完成","records":{"purchaseTime":"2022-11-28T14:52:35.259","purchaseState":"0","consumptionState":"0","developerPayload":"","orderId":"GPA.3314-3542-5698-07117","acknowledgementState":"0","kind":"androidpublisher#productPurchase","regionCode":"SG","packageName":"com.makeup.makeover.fashion.merge","productId":"small_diamonds","checkNum":1}}
    public class GooglePlayCheckInfo
    {
        public int code;
        public string msg = "";
        public Records records = new Records();

        public class Records
        {
            public string purchaseTime = "";
            public string purchaseState = "";
            public string consumptionState = "";
            public string orderId = "";
            public string regionCode = "";
            public string productId = "";

        }
    }
    // { "product_id" : "small_diamonds",  "message" : "{\n  \"data\" : {\n\n  },\n  \"code\" : \"6000\"\n}",  "state" : "1",  "platform" : "ios"}
    public class IOSPlayPurchaseCallBack
    {
        public string platform = "";
        public string product_id = "";
        public string state = "";
        public string message = "";


        internal PurchaseCallBackData toPurchaseCallBack()
        {
            PurchaseCallBackData pcbd = new PurchaseCallBackData();
            pcbd.platform = platform;
            pcbd.product_id = product_id;
            if (message.Contains("6000"))
            {
                IOSCheckInfo iOSCheckInfo = JsonMapper.ToObject<IOSCheckInfo>(message);
                if (iOSCheckInfo.data.transactionId.Length > 0)
                {
                    pcbd.state = PurchaseState.CheckOK;
        
                    pcbd.orderId = iOSCheckInfo.data.transactionId;

                    pcbd.isSandbox = iOSCheckInfo.data.receiptType.ToLower().Contains("sandbox");
                    pcbd.isTrialPeriod = iOSCheckInfo.data.isTrialPeriod.ToLower().Contains("true");
                }
                else
                {
                    pcbd.state = PurchaseState.CheckPayError;
                }

            }
            else
            {
                if (state == "1")
                {
                    pcbd.state = PurchaseState.CheckError;//http ok and checkcode error
                }
                else
                {
                    pcbd.state = PurchaseState.Error;//http error 404?
                }

            }

            return pcbd;


        }
    }
    public class IOSCheckInfo
    {
        public string code;
        public Data data = new Data();

        public class Data
        {
            public string transactionId = "";
            public string originalTransactionId= "";
            public string receiptType=""; //ProductionSandbox
            public string isTrialPeriod = "";
            public string downloadId = "";
        }
    }

    public class UnityIOSPayData
    {
        public string Payload = "";
      
    }
}
